package zozopo.background.operator.operations;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import zozopo.comm.CNSpell;
import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.operations.comm.OperatingService;
import zozopo.warehouse.operations.GoodsclassTB;
import zozopo.warehouse.operations.GoodsclasslistTB;


public class DepartmentOS extends OperatingService{
	
	//private static final Log logger = LogFactory.getLog(DepartmentOS.class);
	
	public DepartmentOS(Connection conn){
		super(conn);
	}
	
	public DepartmentOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean insert(DepartmentTB department){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> insertMap = new HashMap<String, Object>();

		insertMap.put(DepartmentTB.PARENTID_DEPARTMENT, department.getParentid_department());
		insertMap.put(DepartmentTB.NAME_DEPARTMENT, department.getName_department());
		insertMap.put(DepartmentTB.REMARK_DEPARTMENT, department.getRemark_department());
		
		insertMap.put(DepartmentTB.CNSPELL_DEPARTMENT, CNSpell.CNToSpell(department.getName_department()));
		insertMap.put(DepartmentTB.FWCNSPELL_DEPARTMENT, CNSpell.CNToFirstWordSpell(department.getName_department())); 
		
		return sqlMachine.insertRow(DepartmentTB.TABLE_NAME, insertMap);
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<DepartmentTB> selectListBySql(String sql){
		List<DepartmentTB> list = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		if(resultSet != null){
				try {
					list = new ArrayList<DepartmentTB>();
					while(resultSet.next()){
						DepartmentTB department = new DepartmentTB();
						
						department.setCnspell_department(resultSet.getString(DepartmentTB.CNSPELL_DEPARTMENT));
						department.setFwcnspell_department(resultSet.getString(DepartmentTB.FWCNSPELL_DEPARTMENT));
						department.setId_department(resultSet.getInt(DepartmentTB.ID_DEPARTMENT));
						department.setName_department(resultSet.getString(DepartmentTB.NAME_DEPARTMENT));
						department.setParentid_department(resultSet.getInt(DepartmentTB.PARENTID_DEPARTMENT));
						department.setRemark_department(resultSet.getString(DepartmentTB.REMARK_DEPARTMENT));
						
					
				
						list.add(department);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return list;
	
	}
	
	public List<DepartmentTB> select(){
		
		List<DepartmentTB> list = null;
		String sql = "SELECT * FROM " + DepartmentTB.TABLE_NAME ; 
		list = selectListBySql(sql);
		
		return list;
	}
	
	public List<DepartmentTB> select(String sort){
		
		List<DepartmentTB> list = null;
		String sql = "SELECT * FROM " + DepartmentTB.TABLE_NAME 
		+ " ORDER BY " +DepartmentTB.ID_DEPARTMENT+" "+ sort; 
		list = selectListBySql(sql);
		
		return list;
	}
	
	
	public DepartmentTB getDepartment(int id){
		
		String sql = "SELECT * FROM " + DepartmentTB.TABLE_NAME + " where " + DepartmentTB.ID_DEPARTMENT + "=" + id;
		DepartmentTB department = new DepartmentTB();
		List<DepartmentTB> departmentList = selectListBySql(sql);
		department = departmentList.get(0);
		return department;
	}  
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public int getMaxId(){
		//String sql = "select * from "+ReleaseTB.TABLE_NAME+" order by  "+ReleaseTB.RELEASE_DATE+" desc limit "+beginPageNum+","+pageSize; 
		SqlMachine sqlMachine = new SqlMachine(conn);
		int maxId = sqlMachine.getMaxId(DepartmentTB.ID_DEPARTMENT, DepartmentTB.TABLE_NAME);
		return maxId;
	}

	/**
	 * @version 1.0
	 * @author gee
	 */
	public LinkedHashMap<Integer,DepartmentTB>  getMap(){
		
		String sql = "SELECT * FROM " + DepartmentTB.TABLE_NAME;
		List<DepartmentTB> list = selectListBySql(sql);
		
		LinkedHashMap<Integer,DepartmentTB> map = new LinkedHashMap<Integer,DepartmentTB>();
		
		for(DepartmentTB department:list){
			map.put(department.getId_department(),department);
		}
		
		return map;
	}
	
	/**
	 * @version 1.0
	 * @author gee
	 */
	public String  getDepartmentWholename(int id){
		String DepartmentWholename = "";
		String sql = "SELECT * FROM " + DepartmentTB.TABLE_NAME 
		+ " ORDER BY " + DepartmentTB.ID_DEPARTMENT + " DESC";
		List<DepartmentTB> list = selectListBySql(sql);
		
		for(DepartmentTB department:list){
			if(department.getId_department() == id){//如果已不存在
				DepartmentWholename += "-" + department.getName_department();
				id=department.getParentid_department();
			}
		}
		
		return DepartmentWholename;
	}
	
	/**
	 * @version 1.0
	 * @author gee
	 */
	public boolean isSuperior(int superiordeparmentid,int operatordeparmentid){
		
		Map<Integer,DepartmentTB> departmentParentMap = getParentsMap(operatordeparmentid);
		return departmentParentMap.containsKey(superiordeparmentid);
		
	}
	
	/**
	 * @version 1.0
	 * @author gee
	 */
	public Map<Integer,DepartmentTB> getParentsMap(int departmentparentid){
		
		DepartmentTB dpt = getDepartment(departmentparentid);
		int parentid = dpt.getParentid_department();
		
		List<DepartmentTB> departmentList = select("DESC");
		Map<Integer,DepartmentTB> departmentParentMap = new LinkedHashMap<Integer,DepartmentTB>();
		
		for(DepartmentTB department:departmentList){
			if(!departmentParentMap.containsKey(department.getId_department())){//如果已不存在
				if(department.getId_department() == parentid){//如果父ID正常
					departmentParentMap.put(department.getId_department(), department); 
					parentid = department.getParentid_department();
				}
			}
		}
		
		return departmentParentMap;
	}

}
